home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_amanda.idb / usr / freeware / bin / amtoc.z / amtoc
Encoding:
Text File  |  1999-07-16  |  9.3 KB  |  311 lines

  1. #!/usr/bin/perl5
  2.  
  3. # Run perl.
  4. eval '(exit $?0)' && eval 'exec /usr/bin/perl5 -S $0 ${1+"$@"}'
  5.     & eval 'exec /usr/bin/perl5 -S $0 $argv:q'
  6.         if 0;
  7.  
  8. # -------------------------------------------------------
  9. #    Generate a TOC (TableOfContent) for Amanda
  10. #    Release 2 (Amanda 2.3.0 support)
  11. # -------------------------------------------------------
  12.  
  13. # Default paths for this installation of Amanda.
  14. $prefix='/usr/freeware';
  15. $exec_prefix="${prefix}";
  16. $libexecdir="/usr/freeware/libexec";
  17.  
  18. # The directory where configurations can be found.
  19. $config_dir="/usr/freeware/etc/amanda";
  20.  
  21. # Get the version suffix.
  22. $USE_VERSION_SUFFIXES = 'no';
  23. $suf = '';
  24. if ( $USE_VERSION_SUFFIXES =~ /^yes$/i ) {
  25.     $suf='-2.4.1p1';
  26. }
  27.  
  28. $tmpFile="/tmp/amtoc$$";
  29. $= =1000;
  30.  
  31. $stats_t='i3 l i A80 ';
  32. $template='i f3f3 f3f3' . ($stats_t x 10);
  33.  
  34. &init;
  35.  
  36. foreach $current_disk (keys(%method)) {
  37.     if (&read_info($current_disk)) {
  38.       &print_info;
  39.     } else {
  40.       print "no such partition ($current_disk) in database\n" ;
  41.     }
  42. }
  43.  
  44. dbmclose(%curinfo) if ($use_dbm);
  45.  
  46. if (!defined($verbose)) {
  47.     close(TMP);
  48.     open(TMP, "< $tmpFile");
  49.     while (<TMP>) {print;}
  50.     close(TMP);
  51.     unlink($tmpFile);
  52. }
  53.  
  54. # ------------------------------------------------------------------------
  55. sub read_info {
  56. # ------------------------------------------------------------------------
  57.     $info_readed = 0;
  58.     if (! $use_dbm) {
  59.     ($host, $fs)=split (':', $_[0]);
  60.     $fs =~ s,_,__,g;
  61.     $fs =~ s,[/\s],_,g;
  62.     if (open (I, "$infofile/$host/$fs/info")) {
  63.         while (<I>) {
  64.         chomp;
  65.         @field=split;
  66.         if ($field[0] eq "full-rate:") {
  67.             $full_rate[0]=$field[1];
  68.             $full_rate[1]=$field[2];
  69.             $full_rate[2]=$field[3];
  70.         } elsif ($field[0] eq "full-comp:") {
  71.             $full_comp[0]=$field[1];
  72.             $full_comp[1]=$field[2];
  73.             $full_comp[2]=$field[3];
  74.         } elsif ($field[0] eq "incr-rate:") {
  75.             $incr_rate[0]=$field[1];
  76.             $incr_rate[1]=$field[2];
  77.             $incr_rate[2]=$field[3];
  78.         } elsif ($field[0] eq "incr-comp:") {
  79.             $incr_comp[0]=$field[1];
  80.             $incr_comp[1]=$field[2];
  81.             $incr_comp[2]=$field[3];
  82.         } elsif ($field[0] eq "stats:") {
  83.             $size[$field[1]]=$field[2];
  84.             $csize[$field[1]]=$field[3];
  85.             $secs[$field[1]]=$field[4];
  86.             $time[$field[1]]=$field[5];
  87.             $file_no[$field[1]]=$field[6];
  88.             $label[$field[1]]=$field[7];
  89.         }
  90.         }
  91.         $com="unknown";            # ???
  92.         close (I);
  93.         $info_readed=1;
  94.     }
  95.     } elsif (!defined($curinfo{$_[0]."\0"})) {$info_readed=0;}
  96.     else {
  97.     $info_readed=1;
  98.     ($com,
  99.         $full_rate[0],$full_rate[1],$full_rate[2],
  100.         $full_comp[0],$full_comp[1],$full_comp[2],
  101.         $incr_rate[0],$incr_rate[1],$incr_rate[2],
  102.         $incr_comp[0],$incr_comp[1],$incr_comp[2],
  103.         $size[0],$csize[0],$secs[0],$time[0],$file_no[0],$label[0],
  104.         $size[1],$csize[1],$secs[1],$time[1],$file_no[1],$label[1],
  105.         $size[2],$csize[2],$secs[2],$time[2],$file_no[2],$label[2],
  106.         $size[3],$csize[3],$secs[3],$time[3],$file_no[3],$label[3],
  107.         $size[4],$csize[4],$secs[4],$time[4],$file_no[4],$label[4],
  108.         $size[5],$csize[5],$secs[5],$time[5],$file_no[5],$label[5],
  109.         $size[6],$csize[6],$secs[6],$time[6],$file_no[6],$label[6],
  110.         $size[7],$csize[7],$secs[7],$time[7],$file_no[7],$label[7],
  111.         $size[8],$csize[8],$secs[8],$time[8],$file_no[8],$label[8],
  112.         $size[9],$csize[9],$secs[9],$time[9],$file_no[9],$label[9])
  113.       = unpack($template,$curinfo{$_[0]."\0"});
  114.     for ($i=0;$i<10;$i++) { if (defined($label[$i]))
  115.         {$label[$i] =~ s/\0.*//;}
  116.     }
  117.     } # else
  118.     $info_readed;
  119. } # read_info
  120.  
  121. # ------------------------------------------------------------------------
  122. sub print_info {
  123. # ------------------------------------------------------------------------
  124.     if ($verbose) {
  125.     print "$current_disk:";
  126.     print "\tcommand=$com";
  127.     print "\t$comments{$current_disk}\n";
  128.     printf ("\tfull_rate=\t%3.1f\t%3.1f\t%3.1f\n",$full_rate[0],$full_rate[1],$full_rate[2]);
  129.     printf ("\tincr_rate=\t%3.1f\t%3.1f\t%3.1f\n",$incr_rate[0],$incr_rate[1],$incr_rate[2]);
  130.     printf ("\tfull_comp=\t%3.1f%%\t%3.1f%%\t%3.1f%%\n",$full_comp[0]*100,$full_comp[1]*100,$full_comp[2]*100);
  131.     printf ("\tincr_comp=\t%3.1f%%\t%3.1f%%\t%3.1f%%\n",$incr_comp[0]*100,$incr_comp[1]*100,$incr_comp[2]*100);
  132.     print "\n\tlevel\tsize\tcsize\tdur.\tdate\t\tfile_no\tlabel\n";
  133.     for ($i=0;$i<10;$i++) {
  134.     if (defined($size[$i]) && $size[$i]) {
  135.         ($garb,$garb,$garb,$mday,$mon,$year,$garb,$garb,$garb)=localtime($time[$i]);
  136.         $mon++;
  137.         print "\t$i";
  138.         print "\t$size[$i]";
  139.         print "\t$csize[$i]";
  140.         print "\t$secs[$i]";
  141.         print "\t19$year-$mon-$mday";
  142.         print "\t$file_no[$i]";
  143.         print "\t$label[$i]\n";
  144.     } # if
  145.     } # for $i
  146.     print "\n";
  147.     } # if verbose
  148.     else {
  149.     for ($i=0;$i<10;$i++) {
  150.         if (defined($size[$i]) && ($size[$i] ne "") && ($size[$i]>0)
  151.         && ($label[$i] eq $tape_name)) {
  152.         if (!defined($use_tab)) {
  153.             write(TMP);
  154.         }
  155.         else {
  156.             print TMP "\t$file_no[$i]";
  157.             print TMP "\t$current_disk";
  158.             print TMP "\t$i";
  159.             print TMP "\t$size[$i]";
  160.             print TMP "\t$method{$current_disk}";
  161.             print TMP "\t$comments{$current_disk}\n";
  162.         }
  163.         $i=10;    # trick. I don't want to examine amdump files
  164.  
  165.         } # if
  166.     } # for
  167.     } # else
  168. } # print_info
  169.  
  170.  
  171. # ------------------------------------------------------------------------
  172. sub init {
  173. # ------------------------------------------------------------------------
  174.     &usage("amtoc required at least '-c config' option") if ($#ARGV==-1) ;
  175.  
  176.     for ($i=0;$i<=$#ARGV;$i++) {
  177.     if ($ARGV[$i] eq '-c' || ($i == 0 && $ARGV[0] !~ /^-/)) {
  178.         if ($ARGV[$i] eq '-c') {
  179.         $i++;
  180.         &usage("'-c' option require 'config' parameter") if ($i > $#ARGV);
  181.         }
  182.         $conf_name=$ARGV[$i];
  183.  
  184.         -d "$config_dir/$conf_name" ||
  185.         die "$0: configuration directory `$config_dir/$conf_name' does not exist.\n";
  186.         chdir("$config_dir/$conf_name") ||
  187.         die "$0: cannot chdir to `$config_dir/$conf_name': $!\n";
  188.  
  189.             # where to find the directories which hold the
  190.         # administration files
  191.             $infofile=`cd $config_dir/$conf_name && $libexecdir/getconf$suf infofile`;
  192.         chomp $infofile;
  193.  
  194.         if (-d "$infofile") {
  195.         $use_dbm = 0;
  196.         } else {
  197.         $use_dbm = 1;
  198.         }
  199.         if ($use_dbm) {
  200.         dbmopen(%curinfo,"$infofile",400)
  201.             || die ("cannot open dbm_file `$infofile': $!\n");
  202.         }
  203.     }
  204.     elsif ($ARGV[$i] eq '-l') {
  205.         $i++;
  206.         &usage("'-l' option require 'label' parameter") if ($i > $#ARGV);
  207.         $tape_name=$ARGV[$i];
  208.     }
  209.     elsif ($ARGV[$i] eq '-h') {
  210.         $i++;
  211.         &usage("'-h' parameter require 'diskname' parameter") if ($i > $#ARGV);
  212.         $only_host=$ARGV[$i];
  213.     }
  214.     elsif ($ARGV[$i] eq '-v') {$verbose=1;}
  215.     elsif ($ARGV[$i] eq '-f') {$to_file=1;}
  216.     elsif ($ARGV[$i] eq '-i') {$info=1;}
  217.     elsif ($ARGV[$i] eq '-t') {$use_tab=1;}
  218.     else {&usage("no such option $ARGV[$i]");}
  219.     } # all options have been readed
  220.  
  221.     # if ($use_dbm && ! defined (%curinfo)) {&usage("dbmfile empty ?")};
  222.  
  223.     # Read the tapelist.
  224.     $tapelist = `$libexecdir/getconf$suf tapelist`;
  225.     chomp($tapelist);
  226.     $tapelist = 'tapelist' unless $tapelist;
  227.     open(TAPELIST,"< $tapelist")
  228.     || warn "WARNING: cannot open `$tapelist' for reading: $!\n";
  229.  
  230.     if (defined ($tape_name)) {
  231.     # check that $tape_name is defined in tapelist
  232.     $finded=0;
  233.     while (($_=<TAPELIST>) && (!$finded)) {
  234.         ($date,$ltape_name) = split;
  235.         $finded = ($tape_name eq $ltape_name);
  236.     }
  237.     warn "WARNING: $tape_name not in tapelist\n" unless $finded;
  238.     }
  239.     else { # tape_list == top($conf_name/tapelist)
  240.     $_=<TAPELIST>;
  241.     ($date,$tape_name) = split;
  242.     }
  243.     close(TAPELIST);
  244.  
  245.     open(DISKLIST,"< $config_dir/$conf_name/disklist")
  246.     || warn "WARNING: cannot open `disklist': $!\n";
  247.  
  248.     while (<DISKLIST>) {
  249.     if ( (/^[^\#]/) && (/^(\S+)\s+(\S+)\s+(\S+)\s+(.*)/) ) {
  250.                 # host part method comments
  251.         if ((defined ($only_host) && ($1 eq $only_host))
  252.         || !defined($only_host)) {
  253.         $method{$1.":".$2}=$3;
  254.         $comments{$1.":".$2}=$4;
  255.         }
  256.     }
  257.     }
  258.     close(DISKLIST);
  259.  
  260.  
  261.     if (defined($to_file)) {
  262.     close(STDOUT);
  263.     $t=$tape_name;
  264.     $t=~s,[\s/],_,g;
  265.     ($toc=$infofile) =~ s![^/]*$!!;        # remove last path element
  266.     $toc.="$t.toc";
  267.     open(FOUT,"> $toc") ||
  268.         die "Cannot open toc file `$toc' for writing: $!\n";
  269.     select(FOUT) || die "Cannot select output file: $!\n";
  270.     $| = 1; $= =10000; $- = 10000;
  271.     }
  272.  
  273.  
  274.     if (defined($info)) {
  275.     print "AMANDA: To restore:\n";
  276.     print "    position tape at start of file and run:\n";
  277.     print "        dd if=<tape> bs=32k skip=1 [ | zcat ] | restore -...f\n";
  278.     print "    or run:\n";
  279.     print "        amrestore -p <tape> [host [partition]] | restore -...f\n";
  280.     print "\n";
  281.     }
  282.  
  283.     if (!defined($verbose)) {
  284.     if (defined($use_tab)) {
  285.         print "\t#\tpartition\tlevel\tsize\tmethod\n";
  286.         print "\t0\t$tape_name\t-\t-\t$date\n";
  287.     }
  288.     else {
  289.            print "  #  partition                               lvl  size[Kb]  method\n";
  290.        printf("  0  %-41s -         -  %-15s\n",$tape_name,$date);
  291.     }
  292.     open(TMP,"|sort -n > $tmpFile") ||
  293.         die "cannot open temporary file: $!\n";
  294.     $| = 1;
  295.     }
  296. } # init
  297.  
  298.  
  299. # ------------------------------------------------------------------------
  300. sub usage {
  301. # ------------------------------------------------------------------------
  302.     print "@_\n";
  303.     die "USAGE: amtoc [-c] config [-l label] [-h host] [-v] [-i] [-t] [-f]\n";
  304. } # usage
  305.  
  306. # ------------------------------------------------------------------------
  307. format TMP =
  308. @>>  @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @>> @>>>>>>>>  @<<<<<<<<<<<<<<
  309. $file_no[$i],$current_disk,$i,$size[$i],$method{$current_disk}
  310. .
  311.